package com.bible.fire.pad.activity;

import com.bible.fire.R;
import com.bible.fire.local.RuntimeContext;
import com.bible.fire.ui.SimpleProgressDialog;
import com.bible.fire.util.ImageUtil;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class BaseActivity extends Activity {
	
	private ProgressDialog dialog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);		
		initProgressDialog();		
		RuntimeContext.addRuntimeActivity( this );
		this.setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_PORTRAIT );
		//背景图设置为空以调优
		getWindow().setBackgroundDrawable(null);
	}
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {		
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	private void initProgressDialog(){
    	//dialog= new ProgressDialog(this);
    	dialog= new SimpleProgressDialog(this);
        dialog.setMessage("初始化,请等待.....");
        dialog.setIndeterminate(true);
        dialog.setCancelable(true);
        //setDialogText(dialog.getWindow().getDecorView());
    }
	
	private void setDialogText(View v) {
        if (v instanceof ViewGroup) {
                ViewGroup parent = (ViewGroup) v;
                int count = parent.getChildCount();
                for (int i = 0; i < count; i++) {
                        View child = parent.getChildAt(i);
                        setDialogText(child);
                }
        } else if (v instanceof TextView) {
                ((TextView) v).setTextSize(22); //是textview，设置字号
        }
   }
	
	protected void showProgressDialog(){
		dialog.show();
		//setDialogText(dialog.getWindow().getDecorView());
	}
	
	public void dismissProgressDialog(){
		dialog.dismiss();
	}
	
	protected void showAlertDialog( String content ){
		Dialog dialog = new AlertDialog.Builder(this).setTitle("提示").setMessage( content ).setPositiveButton("确定",
	        new DialogInterface.OnClickListener(){
	            public void onClick(DialogInterface dialog, int whichButton){
	                dialog.cancel();
	            }
        }).create();//创建按钮
        dialog.show();
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		super.onOptionsItemSelected( item );
		switch( item.getItemId() ){
			case R.id.menu_exit:
				 System.exit(MODE_APPEND);
				 break;
			case R.id.menu_settings:
				 break;
			case R.id.menu_about:
				 break;
			case R.id.menu_screen_shot:
				 ImageUtil.screenShot( this );
				 break;
		}
		return true;
	}

}
